#include<string>
#include <stdio.h>
#include<vector>
#include <string.h>
#include <mysql/mysql.h>
#include <jsoncpp/json/json.h>
#include"log.h"
#include<iostream>
#include<fstream>

#define HOST "127.0.0.1"
#define PORT 3306
#define USER "root"
#define PASS "147258Ll!"
#define DBNAME "gobang"

class mysql_util{
    //MYSQL *mysql_real_connect(mysql, host, user, pass, dbname, port, unix_socket, flag);
    public:
    static MYSQL* mysql_CREAT(std::string host,std::string user,std::string pass,std::string dbname,int port)
    {
        //初始句柄
        MYSQL * mysql=mysql_init(nullptr);
        if(!mysql)
        {
            ERR_Log("数据库初始化出错: %s",mysql_error(mysql));
            return nullptr;
        }
        //连接数据库
        mysql_real_connect(mysql, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, nullptr,0);
        //设置字符集
        mysql_set_character_set(mysql,"utf8");
        return mysql;


    }
   static bool mysql_exec(MYSQL *mysql, std::string sql)
    {
       int ret= mysql_query(mysql,sql.c_str());
       INF_Log("执行sql语句成功");
        if(ret!=0)
        {
            ERR_Log("执行语句出错:%s",mysql_error(mysql));
            return false;

        }
        return true;


    }
    static bool mysql_destry(MYSQL* mysql)
    {
        mysql_close(mysql);
        return true;
    }

};




class Json_util
{
    public:
    static bool serialize(Json::Value &root,std::string& str)
    {
        //1.工厂
        Json::StreamWriterBuilder swb;
        //2.创建sb
       std::unique_ptr< Json::StreamWriter> sw(swb.newStreamWriter());
        //序列化
        std::stringstream ss;
        int ret=sw->write(root,&ss);
        if(ret<0)
        {
            ERR_Log("序列化出错");
            return false;
        }
        str=ss.str();
        return true;
    }

    static bool unserialize(std::string &str,Json::Value &root)
    {

        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader>cr(crb.newCharReader());
        std::string err;
        bool ret=cr->parse(str.c_str(),str.c_str()+str.size(),&root,&err);
        if(!ret)
        {
            ERR_Log("反序列化出错：%s",err.c_str());
            return false;
        }
        return true;


    }

};


class file_util{
    public:
        static bool read(std::string &filename,std::string &body)
        {
            //1.打开文件
            std::ifstream ifs(filename.c_str(),std::ios::binary);
            if(ifs.is_open()==false)
            {
                ERR_Log("%s文件打开失败",filename.c_str());
                return false;
            }
            //2.获取文件大小
            size_t fsize;
            ifs.seekg(0,std::ios::end);
            fsize=ifs.tellg();
            ifs.seekg(0,std::ios::beg);
            body.resize(fsize);
            //3.读文件
            ifs.read(&body[0],fsize);
            if(ifs.good()==false)
            {
                 ERR_Log("%s文件读取失败",filename.c_str());
                 ifs.close();
                return false;
            }
            //4.关闭文件
            ifs.close();
            return true;



        }

};



class string_util
{
public:
    static void split(std::string &str,std::string &sep,std::vector<std::string>& v)
    {
        size_t pos,idx=0;
        while(idx<str.size())
        {
            pos=str.find(sep,idx);  
            if(pos==std::string::npos)
            {
                
                v.push_back(str.substr(idx));
                break;   
            } 
            else
            {
                if(pos==idx)
                {
                    idx=pos+sep.size();
                    continue;
                   
                }
                 v.push_back(str.substr(idx,pos-idx));
                idx=pos+sep.size();

            }
            

        }
        return ;
        

    }
};

void test_json()
{
    Json::Value root;
    root["name"]="liu";
    root["age"]=18;
    root["score"].append(99);
    root["score"].append(88);
    std::string str;
    Json_util::serialize(root,str);
    INF_Log("序列化结果为：%s",str.c_str());
    Json::Value root2;
    Json_util::unserialize(str,root2);
     std::cout<<"name: "<<root2["name"].asString()<<std::endl;
     std::cout<<"age: "<<root2["age"].asInt()<<std::endl;
     int sz=root2["score"].size();
     for(int i=0;i<sz;i++)
     {
        std::cout<<"score: "<<root2["socre"][i].asFloat()<<std::endl;
     }
     return ;

    

}

void test_mysql()
{
    MYSQL *mysql= mysql_util::mysql_CREAT(HOST,USER,PASS,DBNAME,PORT);
    INF_Log("初始化成功");
    const char * ch="insert stu values(2,'liu','17');";
    mysql_util::mysql_exec(mysql,ch);
    
    mysql_util::mysql_destry(mysql);
    return ;
}

void test_splict()
{
    std::string str="123.13.4567777...4...";
    std::vector<std::string>v;
    std::string sep="..";
    string_util::split(str,sep,v);
    std::cout<<"分割完毕"<<std::endl;
    for(auto e:v)
    {
        std::cout<<"元素："<<e<<std::endl;
    }



}

void test_file()
{
    std::string body;
    std::string filename="./makefile";
    file_util::read(filename,body);
    std::cout<<body<<std::endl;
}


int main()
{
    
    test_file();
    
}